package com.ym.controller.financialManagement;

import com.github.pagehelper.PageInfo;

import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.utils.excel.ExportExcelUtil;

import com.ym.medical.domain.param.ConsumeDetailsParam;
import com.ym.medical.domain.param.DoctorConsumeDetailParam;
import com.ym.medical.domain.param.TransactionDetailsParam;
import com.ym.medical.domain.vo.DoctorConsumeDetailVO;
import com.ym.medical.domain.vo.StudioTransactionVo;
import com.ym.medical.domain.vo.TransactionDetailsVo;
import com.ym.medical.enums.CashFlowEnum;
import com.ym.medical.enums.TransactionDetailsTdTypeEnum;
import com.ym.medical.service.ICommonSV;
import com.ym.medical.service.IConsumeDetailsSV;
import com.ym.medical.service.ITransactionDetailsSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 医生账单管理
 */

@Api(tags = "医生账单管理 - pc")
@RestController
@RequestMapping("/financialManagement/doctor")
@Validated
public class DoctorBillManagementController extends BaseController {

	@Autowired
	private IConsumeDetailsSV consumeDetailsSV;

	@Autowired
	private ITransactionDetailsSV transactionDetailsSV;

	@Autowired
	private ICommonSV commonSV;

	@ApiOperation("医生收入支出记录 (cashFlow 01-收入 02-支出)")
	@GetMapping(value = "/expend/list")
	public APIResponse<PageInfo<DoctorConsumeDetailVO>> listDoctorExpendDetail(
			DoctorConsumeDetailParam doctorConsumeDetailParam,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {
		PageInfo<DoctorConsumeDetailVO> doctorConsumeDetailVOPageInfo = consumeDetailsSV.listDoctorExpendDetail(doctorConsumeDetailParam, pageNo, limit);
		return successData(doctorConsumeDetailVOPageInfo);
	}

	@ApiOperation("医生充值提现记录")
	@GetMapping(value = "/withdraw/list")
	public APIResponse<PageInfo<TransactionDetailsVo>> listDoctorWithdraw(
			TransactionDetailsParam transactionDetailsParam,
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int limit
	) {
		PageInfo<TransactionDetailsVo> transactionDetailsModelPageInfo = transactionDetailsSV.listDoctorWithdraw(transactionDetailsParam, pageNo, limit);
		return successData(transactionDetailsModelPageInfo);
	}

	@ApiOperation("医生提现记录导出")
	@PostMapping("/exportDoctorWithdraws")
	public void exportDoctorWithdraws(HttpServletResponse response,
                                      @RequestBody TransactionDetailsParam transactionDetailsModel,
                                      @RequestParam(defaultValue = "1") int pageNo,
                                      @RequestParam(defaultValue = "10") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医生提现记录";
		String[] headerString = {"序号", "交易单号", "姓名", "手机号", "证件号码", "交易金额", "交易后账户金额", "提现方式", "提现账户", "交易时间"};
		String[] headerField = {"id", "tdNo", "userName", "phoneNo", "cardNo", "tdAmount", "aftTdAmount", "dfAccTypeName", "dfAcc", "tdTime"};

		transactionDetailsModel.setTdType(TransactionDetailsTdTypeEnum.EMBODY.getKey());
		PageInfo<TransactionDetailsVo> transactionDetailsModelPageInfo = transactionDetailsSV.listDoctorWithdraw(transactionDetailsModel, pageNo, limit);
		List<TransactionDetailsVo> list = transactionDetailsModelPageInfo.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, list, response, null);

	}

	@ApiOperation("医生消费收入记录导出")
	@PostMapping("/exportDoctorConsumes")
	public void exportDoctorConsumes(HttpServletResponse response,
                                     @RequestBody DoctorConsumeDetailParam doctorConsumeDetailParam,
                                     @RequestParam(defaultValue = "1") int pageNo,
                                     @RequestParam(defaultValue = "10") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = null;
		String[] headerString = {"序号", "医院", "科室", "交易单号", "订单编号", "工作室", "姓名", "手机号", "证件号码", "付款方式", "付款账户", "交易金额", "交易后账户金额", "问诊患者", "交易时间"};
		String[] headerField = {"id", "hospitalName", "departName", "tdNo", "tradeNo", "dsName", "doctorName", "doctorPhoneNo", "doctorCardNo", "payTypeName", "payAcc", "tdAmount", "aftTdAmount", "patientName", "tdTime"};

		String cashFlow = doctorConsumeDetailParam.getCashFlow();
		if (CashFlowEnum.INCOME.getKey().equals(cashFlow)) {
			titleName = "医生收入记录";
		} else {
			titleName = "医生支出记录";
		}
		doctorConsumeDetailParam.setCashFlow(cashFlow);
		PageInfo<DoctorConsumeDetailVO> doctorConsumeDetailVOPageInfo = consumeDetailsSV.listDoctorExpendDetail(doctorConsumeDetailParam, pageNo, limit);
		List<DoctorConsumeDetailVO> list = doctorConsumeDetailVOPageInfo.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, list, response, null);
	}

	@ApiOperation("工作室交易列表")
	@GetMapping("/studio/transaction")
	@ResponseBody
	public APIResponse studioTransaction(
			@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10", required = false) int limit,
			ConsumeDetailsParam detailsModel) {
		PageInfo<StudioTransactionVo> transactionVos = consumeDetailsSV.studioTransaction(pageNo, limit, detailsModel);
		return successData(transactionVos);

	}

	@ApiOperation("工作室交易导出")
	@PostMapping("/studio/transaction/export")
	public void studioTransactioExport(HttpServletResponse response,
                                       @RequestBody ConsumeDetailsParam model,
                                       @RequestParam(defaultValue = "1", required = true) int pageNo,
                                       @RequestParam(defaultValue = "9999999") int limit) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医生工作室交易账单";
		String[] headerString = {"序号","医院名称", "时间段", "工作室", "消费收入笔数", "消费收入金额", "退费支出笔数", "退费支出金额", "总收入"};
		String[] headerField = {"doctorDsId","hosName", "period", "dsName", "inCount", "inSum", "outCount", "outSum", "income",};
		PageInfo<StudioTransactionVo> transactionVos = consumeDetailsSV.studioTransaction(pageNo, limit, model);
		List<StudioTransactionVo> studioTransactionVos = transactionVos.getList();
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, studioTransactionVos, response, null);

	}



	/*@ApiOperation("医生退费支出记录导出")
	@PostMapping("/exportDoctorRefunds")
	public void exportDoctorRefunds(HttpServletResponse response,@RequestBody
			DoctorConsumeTdVo doctorConsumeTdVo) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医生退费支出记录";
		String[] headerString = {"序号","医院","科室", "交易单号", "订单编号","工作室","姓名", "手机号", "证件号码","付款方式", "付款账户", "交易金额", "交易后账户金额",  "问诊患者","交易时间"};
		String[] headerField = {"id", "hosName","deptName","tdNo","tradeNo", "studioName","name", "phoneNo", "cardNo", "payTypeName", "payAcc","tdAmount", "aftTdAmount", "patientName", "tdTime"};


		List<PatientConsumeTdVo> patientConsumeTdVos = commonSV.queryListEntity(sql.toString(),params,PatientConsumeTdVo.class);
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, patientConsumeTdVos, response, null);
	}*/
	/*@ApiOperation("工作室交易汇总导出")
	@PostMapping("/exportStudioTdStats")
	public void exportStudioTdStats(HttpServletResponse response,@RequestBody
			StudioTdStatsVo studioTdStatsVo) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医生退费支出记录";
		String[] headerString = {"时间段  ","工作室", "提现笔数", "提现金额","消费收入笔数","消费收入金额", "分成消费金额", "退费支出笔数","退费支出金额", "分成退费金额"};
		String[] headerField = {"timePeriod", "studioName","withdrawNum","withdrawAmount","consumeNum", "consumeAmount","fcConsumeAmount", "refundNum", "refundAmount", "fcRefundAmount"};

		List<StudioTdStatsVo> studioTdStatsVos = commonSV.queryListEntity(sql.toString(),params,StudioTdStatsVo.class);
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, studioTdStatsVos, response, null);
	}*/

	/*@ApiOperation("医院交易汇总导出")
	@PostMapping("/exportHosTdStats")
	public void exportHosTdStats(HttpServletResponse response, @RequestBody
			HosTdStatsVo hosTdStatsVo) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医生退费支出记录";
		String[] headerString = {"时间段  ","医院", "提现笔数", "提现金额","消费收入笔数","消费收入金额", "退费支出笔数","退费支出金额", "总收入"};
		String[] headerField = {"timePeriod", "hosName","withdrawNum","withdrawAmount","consumeNum", "consumeAmount", "refundNum", "refundAmount", "totalAmount"};

		List<HosTdStatsVo> hosTdStatsVos = commonSV.queryListEntity(sql.toString(),params,HosTdStatsVo.class);
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, hosTdStatsVos, response, null);
	}*/
}
